package com.md.livingwidget.uiutils;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

import androidx.core.graphics.ColorUtils;

/**
 * shape代码生成
 * <p>
 * /**
 * 创建人：wanggq
 * 创建时间：2019-12-05 15:14
 */

public class ShapeUtils {

    /***
     * 获取shape背景
     *
     * @param strokeWidth -- 描边色
     * @param roundRadius -- 圆角大小
     * @param strokeColor -- 描边色
     * @param fillColor   -- 描边色
     * @return
     */
    public static StateListDrawable getDrawableList(int strokeWidth, int roundRadius, int strokeColor, int fillColor) {
        return getDrawableList(strokeWidth, new float[]{roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius}, strokeColor, fillColor);
    }

    public static StateListDrawable getDrawableList(int strokeWidth, float[] radii, int strokeColor, int fillColor) {
        StateListDrawable drawable = new StateListDrawable();

        GradientDrawable pressState = new GradientDrawable();//创建drawable
        pressState.setColor(caculateColor(fillColor, Color.BLACK, 0.1f));
        pressState.setCornerRadii(radii);
        pressState.setStroke(strokeWidth, strokeColor);

        GradientDrawable eableState = new GradientDrawable();//创建drawable
        eableState.setColor(caculateColor(fillColor, Color.WHITE, 0.5f));
        eableState.setCornerRadii(radii);
        eableState.setStroke(0, strokeColor);

        GradientDrawable commonState = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{Color.BLACK, fillColor});
        commonState.setColor(fillColor);
        commonState.setCornerRadii(radii);
        commonState.setStroke(strokeWidth, strokeColor);

        drawable.addState(new int[]{-android.R.attr.state_enabled}, eableState);
        drawable.addState(new int[]{android.R.attr.state_pressed}, pressState);
        drawable.addState(new int[]{}, commonState);

        return drawable;
    }


    public static StateListDrawable getDrawableList(int strokeWidth, float topLeftRadius, float topRightRadius, float bottomRightRadius, float bottomLeftRadius, int strokeColor, int fillColor) {
        float[] radii = new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius};
        StateListDrawable drawable = new StateListDrawable();

        GradientDrawable pressState = new GradientDrawable();//创建drawable
        pressState.setColor(caculateColor(fillColor, Color.BLACK, 0.1f));
        pressState.setCornerRadii(radii);
        pressState.setStroke(strokeWidth, strokeColor);

        GradientDrawable eableState = new GradientDrawable();//创建drawable
        eableState.setColor(caculateColor(fillColor, Color.WHITE, 0.5f));
        eableState.setCornerRadii(radii);
        eableState.setStroke(0, strokeColor);

        GradientDrawable commonState = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{Color.BLACK, fillColor});
        commonState.setColor(fillColor);
        commonState.setCornerRadii(radii);
        commonState.setStroke(strokeWidth, strokeColor);


        drawable.addState(new int[]{-android.R.attr.state_enabled}, eableState);
        drawable.addState(new int[]{android.R.attr.state_pressed}, pressState);
        drawable.addState(new int[]{}, eableState);

        return drawable;
    }

    /**
     * 获取按钮 normal press diassable 颜色点击色
     *
     * @param strokeRoundWidth
     * @param roundRadius
     * @param strokeColor
     * @param
     * @return
     */
    public static StateListDrawable getPHButtonDrawableList(int strokeRoundWidth, float roundRadius, int strokeColor, int nomalColor, int pressColor, int eableColor) {
        float[] radii = new float[]{roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius};
        StateListDrawable drawable = new StateListDrawable();

        GradientDrawable pressState = new GradientDrawable();//创建drawable
        pressState.setShape(GradientDrawable.RECTANGLE);
        pressState.setColor(pressColor);
        pressState.setCornerRadii(radii);
        pressState.setStroke(strokeRoundWidth, strokeColor);

        GradientDrawable eableState = new GradientDrawable();//创建drawable
        eableState.setShape(GradientDrawable.RECTANGLE);
        eableState.setColor(eableColor);
        eableState.setCornerRadii(radii);
        eableState.setStroke(strokeRoundWidth, caculateColor(strokeColor, Color.WHITE, 0.4f));


        GradientDrawable commonState = new GradientDrawable();
        commonState.setShape(GradientDrawable.RECTANGLE);
        commonState.setColor(nomalColor);
        commonState.setCornerRadii(radii);
        commonState.setStroke(strokeRoundWidth, strokeColor);

        drawable.addState(new int[]{-android.R.attr.state_enabled}, eableState);
        drawable.addState(new int[]{android.R.attr.state_pressed}, pressState);

        drawable.addState(new int[]{}, commonState);

        return drawable;
    }

    /**
     * 获取按钮 normal press diassable 颜色点击色
     *
     * @param strokeRoundWidth
     * @param roundRadius
     * @param strokeColor
     * @param fillColor
     * @return
     */
    public static StateListDrawable getPHButtonDrawableList(int strokeRoundWidth, float roundRadius, int strokeColor, int fillColor) {
        float[] radii = new float[]{roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius};
        StateListDrawable drawable = new StateListDrawable();

        GradientDrawable pressState = new GradientDrawable();//创建drawable
        pressState.setColor(caculateColor(fillColor, Color.BLACK, 0.1f));
        pressState.setCornerRadii(radii);
        pressState.setStroke(strokeRoundWidth, strokeColor);

        GradientDrawable eableState = new GradientDrawable();//创建drawable
        eableState.setColor(caculateColor(fillColor, Color.WHITE, 0.5f));
        eableState.setCornerRadii(radii);
        eableState.setStroke(0, strokeColor);

        GradientDrawable commonState = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{Color.BLACK, fillColor});
        commonState.setColor(fillColor);
        commonState.setCornerRadii(radii);
        commonState.setStroke(strokeRoundWidth, strokeColor);

        drawable.addState(new int[]{-android.R.attr.state_enabled}, eableState);
        drawable.addState(new int[]{android.R.attr.state_pressed}, pressState);
        drawable.addState(new int[]{}, commonState);

        return drawable;
    }

    public static GradientDrawable getPHButtonGradientDrawable(int strokeRoundWidth, int roundRadius, int fillColor) {
        float[] radii = new float[]{roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius};
        GradientDrawable commonState = new GradientDrawable();
        commonState.setColor(fillColor);
        commonState.setCornerRadii(radii);
//        commonState.setStroke(strokeRoundWidth, strokeColor);
        return commonState;
    }


    public static GradientDrawable getPHButtonGradientDrawable(int strokeRoundWidth, int roundRadius, int strokeColor, int fillColor) {
        float[] radii = new float[]{roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius, roundRadius};
        GradientDrawable commonState = new GradientDrawable();
        commonState.setColor(fillColor);
        commonState.setCornerRadii(radii);
        commonState.setStroke(strokeRoundWidth, strokeColor);
        return commonState;
    }


    /**
     * 对TextView设置不同状态时其文字颜色
     *
     * @param normal
     * @param pressed
     * @param selected
     * @return
     */
    public static ColorStateList getColorStateList(int normal, int pressed, int selected) {
        int[] colors = new int[]{pressed, selected, normal};
        int[][] states = new int[3][];
        states[0] = new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{android.R.attr.state_selected};
        states[2] = new int[]{};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

    /**
     * 对TextView设置不同状态时其文字颜色。
     *
     * @param normal
     * @param pressed
     * @param focused
     * @param selected
     * @param unable
     * @return
     */
    public static ColorStateList getColorStateList(int normal, int pressed, int focused, int selected, int unable) {
        int[] colors = new int[]{pressed, focused, selected, unable, normal};
        int[][] states = new int[5][];
        states[0] = new int[]{android.R.attr.state_pressed};
        states[1] = new int[]{android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_selected};
        states[3] = new int[]{-android.R.attr.state_enabled};
        states[4] = new int[]{};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }

//    public static ColorStateList getColorStateList(int normal, int pressed, int focused, int selected, int unable) {
//        int[] colors = new int[]{pressed, focused, normal, focused, selected, unable, normal};
//        int[][] states = new int[7][];
//        states[0] = new int[]{android.R.attr.state_pressed};
//        states[1] = new int[]{android.R.attr.state_focused};
//        states[2] = new int[]{android.R.attr.state_enabled};
//        states[3] = new int[]{android.R.attr.state_focused};
//        states[4] = new int[]{android.R.attr.state_window_focused};
//        states[6] = new int[]{};
//        ColorStateList colorList = new ColorStateList(states, colors);
//        return colorList;
//    }


    /**
     * 计算从startColor过度到endColor过程中百分比为franch时的颜色值
     *
     * @param startColor 起始颜色 int类型
     * @param endColor   结束颜色 int类型
     * @param franch     franch 百分比 0.5
     * @return 返回int格式的color
     */
    public static int caculateColor(int startColor, int endColor, float franch) {
        return ColorUtils.blendARGB(startColor, endColor, franch);
    }


}
